//
//  EssenceContentViewController.swift
//  HLSwiftWork
//
//  Created by hulinhu on 2018/6/12.
//  Copyright © 2018年 hulinhu. All rights reserved.
//

import UIKit

fileprivate let indentifier = "cell"

class EssenceContentViewController: HLBaseViewController {
    
    var a: String {
        return parent.self!.isKind(of: NewsViewController.self) ? "newlist":"list"
    }
    var contents = [ESContentModel]()
    var type: ContentType = .all
    var maxTime: String = ""
    var currentIndex = 0
    var page: Int = 0
  
    
    
    // 初始化 tableView
    lazy var tableView : UITableView = {
        // self 必须加上
        let sframe = CGRect(x: 0, y: kTopHeight+35, width: self.view.zc_width , height: kScreenHeight-kTopHeight-kTabBarHeight-35)
        let tabView = UITableView(frame:sframe , style:.plain)
        tabView.delegate = self
        tabView.dataSource = self
//        tabView.separatorStyle = .none
        tabView.showsVerticalScrollIndicator = false
        return tabView
        
     }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
         setupTabUI()
         loadContents()
    }

}
// MARK: - 设置UI界面
extension EssenceContentViewController{
    fileprivate  func  setupTabUI(){
        
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
        }
       tableView.register(UINib.init(nibName: "ContentCell", bundle: nil), forCellReuseIdentifier: indentifier)
        tableView.separatorStyle = .none
        
        view.addSubview(tableView)
        
        tableView.reloadData()
    }
}

// MARK: - 请求数据
extension EssenceContentViewController {
    
    fileprivate func loadContents() {
        page += 1
        EssenceNewViewModel.loadContentsNow(a: a, type: type.rawValue, page: page, maxTime: maxTime) { (isSuccess, maxTime, dataArray) in
            if isSuccess{
                self.maxTime = maxTime
                self.contents += dataArray
                self.tableView.reloadData()
            }else{
                self.page -= 1
            }
        }
    }
}

// MARK: - dataSource
extension EssenceContentViewController: UITableViewDataSource {
    
   func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return contents.count
    }
    
   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cell = tableView.dequeueReusableCell(withIdentifier: indentifier) as! ContentCell
         cell.content = contents[indexPath.row]
    
//        if cell.content.type == ContentType.video.rawValue {
//            
//           cell.videoView.palyButton.tag = indexPath.row
//         cell.videoView.palyButton .addTarget(self, action: #selector(videoBtnClick(_:)), for:.touchUpInside)
//            
//        }else if cell.content.type == ContentType.voice.rawValue {
//            cell.voiceView.palyButton.tag = indexPath.row
//             cell.voiceView.palyButton .addTarget(self, action: #selector(voiceBtnClick(_:)), for:.touchUpInside)
//          }
    
        if indexPath.row == contents.count - 1 {
            self.loadContents()
        }
        return cell
    }
}

// MARK: - delegate
extension EssenceContentViewController : UITableViewDelegate{
    
   func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return contents[indexPath.row].cellHeight
    }
    
   func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = CommentViewController()
        vc.content = contents[indexPath.row]
        navigationController?.pushViewController(vc, animated: true)
    }
}
// MARK: - 事件点击
extension EssenceContentViewController  {
    
    @objc fileprivate func videoBtnClick(_ sender: UIButton){
        
        let contentModel = contents[sender.tag]
        NSLog("视频点击 ---- URL == \(contentModel.videouri)")
//        let vc = CommentViewController()
//        vc.content = contents[sender.tag]
//        navigationController?.pushViewController(vc, animated: true)
    }
    
    @objc fileprivate func voiceBtnClick(_ sender: UIButton){
        NSLog("音频点击")
    }
    

}
